package com.shujia.wyh.utils;

import java.io.BufferedReader;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;

/*
    编写一个工具类获取连接对象
 */
public class MysqlTool {
    private MysqlTool(){}

    public static Connection getConnection(){
        Connection conn = null;
        try {
            //1、加载数据库的驱动
            Class.forName("com.mysql.jdbc.Driver");

            //读取配置文件获取url,用户名和密码
            Properties prop = new Properties();
            prop.load(new BufferedReader(new FileReader("E:\\projects\\IDEAProjects\\bigdata23-project\\bigdata23-project\\bigdata23-jdbc\\src\\main\\java\\com\\shujia\\wyh\\utils\\mysqlinfo.properties")));
            String url = prop.getProperty("url");
            String username = prop.getProperty("username");
            String password = prop.getProperty("password");

            //2、获取数据库的连接对象
            conn = DriverManager.getConnection(url, username, password);

        }catch (Exception e){
            e.printStackTrace();
        }

        if(conn!=null){
            System.out.println("与数据连接建立成功！！");
        }else {
            System.out.println("与数据连接建立失败！！");
        }

        return conn;
    }

}
